import {handleChildren} from "@/router";
import {getDynaMenu, getMenu, getMenuSider} from "@/menu";

/**
 * 标准框架，包括：
 * 1，设置配置
 * 2，语言种类
 * 3，导航菜单
 * 4，多页tabs控制
 * */
export default {
	namespaced: true,
	state: {
		menu: [],//导航
		opened: [],
		currentFrame: '',
		currentPage: null,
		currentAccount: null,
		isMobile: false,
		activeMenuName:'',
		openMenuName:[],
		noticeTabList:[
			{
				title: "通知",
				name: "message",
				status: "0",  //消息状态 '0'未读，'1'已读，'2'已清空
				messageLoading: false,
				hasLoadedAll: false,
				count:0,
				messageList:[]
			},
			{
				title: "已读",
				name: "doneMessage",
				status: "1", //消息状态 '0'未读，'1'已读，'2'已清空
				messageLoading: false,
				hasLoadedAll: false,
				count:0,
				messageList: [],
			},
		]
	},
	getters: {},
	mutations: {
		setMenu(state, menu) {
			state.menu = [...state.menu, ...menu]
		},
		setCurrentAccount(state,api){
			if(state.currentAccount!=null){
				return;
			}
			let token = util.cookies.get('token');
			let uuid =  util.cookies.get('uuid');
			if (token && uuid) {
				let ids=[uuid]
				api.AccountUser.queryByIds(ids).then(res => {
					if(res && res.length>0){
						state.currentAccount = res[0];
					}
				})
			}
		},
		setDevice(state, isMobile) {
			state.isMobile = isMobile;
		},
		setNotice(state, noticeTabList){
			state.noticeTabList = noticeTabList;
		},
		setActiveMenuName(state, activeMenuName){
			state.activeMenuName = activeMenuName;
		},
		setOpenMenuName(state, openMenuName){
			state.openMenuName = openMenuName;
		},
	},
	actions: {
		/**
		 * login方法
		 * */
		async login({state, rootState,commit}, {values,router}) {
			let param = {
				account: values.username,
				password: rootState.tools.md5(values.password + rootState.api.AccountUser.getSlat())
			};
			let resp = await rootState.api.AccountUser.login(param);
			state.currentAccount = resp.data;
			await rootState.tools.cookies.set('uuid', resp.data.id);
			await rootState.tools.cookies.set('token', resp.token);
			let menuList = await getDynaMenu(rootState.api)
			let menuSider = getMenuSider(menuList);
			state.menu =[];
			let menu=getMenu();
			commit('setMenu',[...menu,...menuSider]);
			let array = handleChildren(menuSider,[]);
			array.map(a=>router.$addRoutes(a))
			return resp.data;
		},
		async init({state,rootState}) {
			state.opened = [];
			state.currentPage = null;
			state.currentAccount = null;
			let token = rootState.tools.cookies.get('token');
			await rootState.api.BaseSso.logout(token);
			await rootState.tools.cookies.remove('token');
			await rootState.tools.cookies.remove('uuid');
		},
		addPage({state, rootState}, page) {
			let {fullPath, meta, matched, path} = page;
			let index = state.opened.findIndex(item => item.fullPath === page.fullPath);
			if (index == -1) {
				state.opened.push({fullPath, meta, matched, path});
			}
			state.currentPage = {fullPath, meta, matched, path};
		},
		removePage({state, rootState}, pageName) {
			let index = state.opened.findIndex(page => page.fullPath === pageName);
			if (index === -1) return false;
			if (state.opened.length === 1) {
				state.currentPage = null;
			} else {
				if (index < state.opened.length - 1) {
					state.currentPage = state.opened[index + 1]
				} else {
					state.currentPage = state.opened[index - 1]
				}
			}
			;
			state.opened.splice(index, 1);
			return state.currentPage
		},
		setPage({state, rootState}, pageName) {
			let index = state.opened.findIndex(page => page.fullPath === pageName);
			if (index !== -1) {
				state.currentPage = state.opened[index];
				return state.currentPage;
			}
			return false;
		},
		movePage({state, rootState}, {name, newName, a, b}) {
			state.opened.splice(b, 1, ...state.opened.splice(a, 1, state.opened[b]));
		}
	}
}
